﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Quality.BasicData;
namespace Quality.Records
{
    public partial class add : Form
    {
        public add()
        {
            InitializeComponent();
            DB.getcombobox(shift, "shift", "num", "shiftname");
            num.Text =sqlHelper.getnum("recodersnum");
        }
      
        private void button2_Click(object sender, EventArgs e)
        {

            selecteq frm = new  selecteq();

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEvent += frm_TransfEvent;
            frm.Show();
        }
        //事件处理方法
        void frm_TransfEvent(string[] value)
        {
            eqnum.Tag = value[0];
            eqnum.Text = value[1];

          

        }

        private void button1_Click(object sender, EventArgs e)
        {
            selectpro frm = new selectpro();

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEvent += frm_TransfEvent1;
            frm.Show();
        }
        //事件处理方法
        void frm_TransfEvent1(string[] value)
        {
            pronum.Text = value[0];
            proname.Text = value[1];
            typenum.Text = value[2];
            typenum.Tag = value[3];

            DataTable dt = sqlHelper.ExecuteDataTable("select * from assigneditems a left join testitems b on a.itemnum=b.num where a.typenum=" + value[3] + "");
            listBox1.DataSource = dt;
            listBox1.DisplayMember = "itemname";
            listBox1.ValueMember = "num";
            listBox1.SelectedIndex = -1;

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                DataTable dt = sqlHelper.ExecuteDataTable("select * from assignedindicators a left join testindicators b on a.indnum=b.num where a.itemnum='" + listBox1.SelectedValue.ToString() + "'");
                listBox2.DataSource = dt;
                listBox2.DisplayMember = "indname";
                listBox2.ValueMember = "num";
                listBox2.SelectedIndex = -1;
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            label11.Text = "";
            if (listBox2.SelectedIndex != -1)
            {
                DataTable dt = sqlHelper.ExecuteDataTable("select * from  testindicators  where num='" + listBox2.SelectedValue.ToString() + "'");
                label11.Text = dt.Rows[0]["indvalue"].ToString();
           
                input.Focus();

                DataTable dt1 = sqlHelper.ExecuteDataTable("select rownum,testvalue from  records  where num='" + num.Text + "'and itemnum='" + listBox1.SelectedValue + "'and  indnum='" + listBox2.SelectedValue + "'");
                listBox3.DataSource = dt1;
                listBox3.DisplayMember = "testvalue";
                listBox3.ValueMember = "rownum";
                listBox3.SelectedIndex = -1;
            }

        }

        private void input_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Control || e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(proname.Text))
                {
                    MessageBox.Show("产品不能为空！");
                    return;

                }
                if (string.IsNullOrEmpty(typenum.Text))
                {
                    MessageBox.Show("检验类型不能为空！");
                    return;

                }
                if (listBox1.SelectedIndex == -1) {
                    MessageBox.Show("检验项目不能为空！");
                    return;
                
                }
                if (listBox2.SelectedIndex == -1)
                {
                    MessageBox.Show("检验指标不能为空！");
                    return;

                }

                sqlHelper.ExecuteNonQuery("insert into records (num,rownum,workdate,shift,pronum,proname,typenum,eqnum,batch,itemnum,indnum,testvalue,making,makingdate)values('" + num.Text + "','" + DateTime.Now.ToString("yyyyMMddHHmmss") + "','" + workdate.Value + "','" + shift.Text + "','" + pronum.Text + "','" + proname.Text + "','" + typenum.Tag + "','" + eqnum.Tag + "','" + batch.Text + "','" + listBox1.SelectedValue + "','" + listBox2.SelectedValue + "','" + input.Text + "','" + sqlHelper.loginname + "','" + DateTime.Now.ToString() + "')");

                DataTable dt = sqlHelper.ExecuteDataTable("select rownum,testvalue from  records  where num='" + num.Text + "' and itemnum='" + listBox1.SelectedValue + "' and indnum='" + listBox2.SelectedValue + "'");
            listBox3.DataSource = dt;
            listBox3.DisplayMember = "testvalue";
            listBox3.ValueMember = "rownum";
            listBox3.SelectedIndex = -1;
            input.Text = "";
            input.Focus();

            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox3.SelectedIndex != -1) {

                sqlHelper.ExecuteNonQuery("delete from records where rownum='" + listBox3.SelectedValue + "'");
            
              DataTable dt = sqlHelper.ExecuteDataTable("select rownum,testvalue from  records  where num='" + num.Text + "'");
            listBox3.DataSource = dt;
            listBox3.DisplayMember = "testvalue";
            listBox3.ValueMember = "rownum";
            input.Focus();
            }
        }

     

       

        

       
    }
}
